const utils = require('../../utils/utils.js');
const api = require('../../utils/api.js');
const indexStore = require('../../service/indexStore.js');

const goods = [{
  src:'../../images/goods4.jpg',
  },{
    src: '../../images/goods5.jpg'
  },{
    src: '../../images/goods6.jpg'
  },{
    src: '../../images/goods7.jpg'
  }, {
    src: '../../images/goods5.jpg'
  }, {
    src: '../../images/goods6.jpg'
  }, {
    src: '../../images/goods7.jpg'
  }]


const submenu_right = [{
  src: '../../images/goods4.jpg',
}, {
  src: '../../images/goods5.jpg'
}, {
  src: '../../images/goods6.jpg'
}, {
  src: '../../images/goods7.jpg'
}, {
  src: '../../images/goods5.jpg'
}, {
  src: '../../images/goods6.jpg'
}, {
  src: '../../images/goods7.jpg'
}]

const top_menu = [
  { title: '评价'},
  { title: '商家'}];

/**
 * submenu_left的元素是一个对象,有title和list两个属性
 * title {string} 子菜单标题
 */
const submenu_left = [{ title: '买过' }, { title: '热销' }, { title: '优惠' }, { title: '烤小海鲜类' }];


// pages/details/index.js
Page({
  onLoad:function(options){
    let t =indexStore.getter();
    console.log('details:',t);
    this.generateGoods();
    this.generate_top_menu();
    this.generateSubmenu_left();
    this.generateSubmenu_right();
    utils.getElTop('.submenu-left').then((result)=>{
      this.setData({ submenuLeft: { top: result.top}});
     })
  },
  onPageScroll: function (e) {
    // const onPageScroll_pending = this.data.onPageScroll_pending;
    // console.log(onPageScroll_pending);
    // if (onPageScroll_pending) {
    //   console.log('onPageScroll_pending');
    //   setTimeout(() => { this.setData({ onPageScroll_pending: false })},3000);
    
    //   return ;
    // }
    // const submenuLeft = this.data.submenuLeft;
    // const top = submenuLeft.top;
    // const isOk = (e.scrollTop >= top);
    // console.log('isOk:',isOk);
    // if(isOk){
    //   this.setData({ onPageScroll_pending: true });
      // wx.pageScrollTo({ 
    //     scrollTop: submenuLeft.top,
    //     duration:0
    //     });
    // }
  },
  /**
   * 页面的初始数据
   */
  data: {
    goods,
    top_menu,
    submenu_left,
    submenu_right,
    isLockDowo:false,
    onPageScroll_pending:false,
    submenuLeft:{}
  },
  /**
   * 生成submenu_left所需的数据
   */
  generateSubmenu_left:function(){
    const length = submenu_left.length;
    const _submenu_left = [];
    for (let ii = 0; ii < 8; ii++)
    for(let i=0;i<length;i++){
      const title = submenu_left[i].title;
      const id = i;
      _submenu_left.push({ id, title});
    }
    this.setData({ submenu_left: _submenu_left});
  },
  /**
   * 生成submenu_right所需的数据
   */
  generateSubmenu_right:function(){
    const length = submenu_right.length;
    const _submenu_right = [];
    const _price_module_data = [];
    const isOk = [true, false];
    for(let i=0;i<length;i++){
      const src = submenu_right[i].src;
      const title = '碳烤生蚝6只';
      const goods_abstract = '满口糯糯的感觉,好吃到爆炸!';
      const isDiscount = isOk[Math.floor(Math.random() * isOk.length)];
      const price = (Math.random() * 100).toFixed(1);
      const rate = Math.random() * 0.8;
      const discount_rate = (rate * 10).toFixed(1);
      const discount_price = (price * rate).toFixed(1);
      const sell_amount = Math.floor(Math.random() * 1000);
      _submenu_right.push({ src, title, goods_abstract, isDiscount, price, discount_rate, discount_price, sell_amount});
    }
    this.setData({ submenu_right: _submenu_right, price_module_data: _price_module_data});
  },
  /**
   * 生成goods的数据
   */
  generateGoods:function(){
    const length = goods.length;
    const _goods = [];
    const isOk = [true,false];
    for(let i=0;i<length;i++){
      const src = goods[i].src;
      const isDiscount = isOk[Math.floor(Math.random() * isOk.length)];
      const price =(Math.random()*100).toFixed(1);
      const discount_price = (price * Math.random()*0.8).toFixed(1);
      const sell_amount = Math.floor(Math.random() * 1000);
      _goods.push({ src, isDiscount, price, discount_price, sell_amount});
    }
    this.setData({ goods: _goods});
  },
  /**
   * 生成top_menu的数据
   */
  generate_top_menu:function(){
    const length = top_menu.length;
    const _top_menu = [];
    for(let i=0;i<length;i++){
      _top_menu.push({
        title: top_menu[i].title,
        isDecorate:false,
        order: String((i + 1))
      })
    }
    this.setData({ top_menu: _top_menu});
  },

  /**
   * 改变所有top_menu数组元素中isDecorate属性的值
   * 点击元素isDecorate变为true,其余的变为false
   * @param e {Event}
   */
  tiggerDecorate:function(e){
    const _index = e.currentTarget.dataset.index;
    const top_menu = this.data.top_menu;
    top_menu.forEach((val, index)=>{
      if (index === _index){
        val.isDecorate = true;
        val.order = top_menu.length + 1;
      }else{
        val.isDecorate = false;
        val.order = index;
      }
      this.setData({ top_menu})
    })
  }
})